<template>
  <div class="rightbox" v-cloak>
    <h1>首页banner</h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" label-width="110px">
        <el-form-item
          prop="colour"
          label="焦点选中颜色"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-color-picker v-model="form.colour"></el-color-picker>
        </el-form-item>
        <el-form-item
          label="原图片宽度px"
          prop="width"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.width"></el-input>
        </el-form-item>
        <el-form-item
          label="原图片高度px"
          prop="height"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.height"></el-input>
        </el-form-item>
        <el-form-item
          label="轮播时间(秒)"
          prop="interval"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.interval"></el-input>
        </el-form-item>
        <div v-for="(item, index) in form.pictureList" v-bind:key='index'>
          <el-form-item
            v-bind:label="'banner'+(index+1)"
            :prop="'pictureList.' + index + '.value'"
            :rules="[
              { required: true, message: '不能为空', trigger: 'blur' }
            ]"
          >
            <div class="upload-box">
              <span>点击上传</span>
              <input type="text" disabled="" id="textfield" v-model="item.value" style="display:none;">
              <img style="float:left;width:320px;margin-left:20px;" v-bind:src="item.value"/>
              <input type="file" style="float:left;"  @change="getFile(item,$event)" v-bind:label="item.name" class="fileinput">
            </div>
            <el-button style="float:left;margin-left:20px;" @click="removeDomain(index,item)">删除</el-button>
          </el-form-item>
<!--           <el-form-item
            v-bind:label="'banner'+(index+1)"
            :prop="'pictureList.' + index + '.value'"
            :rules="[
              { required: true, message: '不能为空', trigger: 'blur' }
            ]"
          >
            <div class="upload-box">
              <el-input type="file" style="float:left;" v-model="item.value" v-bind:label="item.name" class="fileinput"></el-input>
            </div>
            <el-button style="float:left;margin-left:20px;" @click="removeDomain(index,item)">删除</el-button>
          </el-form-item> -->
        </div>
        <el-form-item>
          <el-button type="primary" v-if="bannerstate == 1" @click="submit('form')">提交</el-button>
          <el-button type="primary" v-if="bannerstate == 2" @click="submit2('form')">修改提交</el-button>
          <el-button @click="addimg">新增图片</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
// import qs from 'qs'
import $ from 'jquery'
export default {
  data () {
    return {
      logining: false,
      form: {
        colour: '',
        height: '',
        interval: '',
        width: '',
        pictureList: []
      },
      fileList2: [],
      bannerstate: ''
    }
  },
  methods: {
    getFile (item, $event) {
      var timestamp = new Date().getTime()
      item.name = 'modi' + timestamp
      var file = $event.currentTarget.files[0]
      var reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = function () {
        item.value = this.result
      }
    },
    addimg: function () {
      var timestamp = new Date().getTime()
      this.form.pictureList.push({
        index: 0,
        type: 2,
        value: '',
        name: 'img' + timestamp
      })
    },
    removeDomain (index, item) {
      var inde = this.form.pictureList.indexOf(item)
      if (inde !== -1) {
        this.form.pictureList.splice(inde, 1)
      }
    },
    getList: function () {
      var that = this
      that.$pub.open()
      axios({
        method: 'post',
        url: '/gp/Banner/Index'
      })
        .then((res) => {
          that.$pub.close()
          if (res.data.code === 0) {
            that.form.colour = res.data.data.colour
            that.form.height = res.data.data.height
            that.form.interval = res.data.data.interval
            that.form.width = res.data.data.width
            if (res.data.data.pictureList) {
              that.form.pictureList = res.data.data.pictureList
              for (var i = 0; i < that.form.pictureList.length; i++) {
                that.form.pictureList[i].name = ''
              }
              that.bannerstate = 2
            } else {
              that.bannerstate = 1
            }
          } else {
            that.$pub.failcode(that, res.data.code, res.data.data)
          }
        })
        .catch(function () {
          that.$pub.catchbox(that)
        })
    },
    submit: function (form) {
      var that = this
      var formFile = new FormData()
      that.$refs[form].validate((valid) => {
        if (valid) {
          $('.fileinput').each(function () {
            formFile.append($(this).attr('label'), $(this).prop('files')[0])
          })
          for (var i = 0; i < that.form.pictureList.length; i++) {
            that.form.pictureList[i].value = ''
          }
          formFile.append('colour', that.form.colour)
          formFile.append('height', that.form.height)
          formFile.append('interval', that.form.interval)
          formFile.append('width', that.form.width)
          formFile.append('fileModel', JSON.stringify(that.form.pictureList))
          $.ajax({
            type: 'post',
            url: '/gp/Banner/AddPost',
            dataType: 'json',
            data: formFile,
            processData: false,
            contentType: false,
            success: function (data) {
              if (data.code === 0) {
                that.$alert('新增成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    that.getList()
                  }
                })
              } else {
                that.$pub.failcode(that, data.code, data.data)
              }
            },
            error: function (data) {
              that.$pub.catchbox(that)
            }
          })
        }
      })
    },
    submit2: function (form) {
      var that = this
      var formFile = new FormData()
      that.$refs[form].validate((valid) => {
        if (valid) {
          $('.fileinput').each(function () {
            formFile.append($(this).attr('label'), $(this).prop('files')[0])
          })
          for (var i = 0; i < that.form.pictureList.length; i++) {
            that.form.pictureList[i].value = ''
          }
          formFile.append('colour', that.form.colour)
          formFile.append('height', that.form.height)
          formFile.append('interval', that.form.interval)
          formFile.append('width', that.form.width)
          formFile.append('fileModel', JSON.stringify(that.form.pictureList))
          $.ajax({
            type: 'post',
            url: '/gp/Banner/EidtPost',
            dataType: 'json',
            data: formFile,
            processData: false,
            contentType: false,
            success: function (data) {
              if (data.code === 0) {
                that.$alert('修改成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    that.getList()
                  }
                })
              } else {
                that.$pub.failcode(that, data.code, data.data)
              }
            },
            error: function (data) {
              that.$pub.catchbox(that)
            }
          })
        }
      })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
